import Vue from 'vue';
import utils from "@common/utils/utils";
import GlobalDialog from "../global";
import ExcelForm from "./ExcelForm";

const defaultProps = {
	fileName: '',
	success: false,
	error: false,
	errorMsg: '',
	errorFile: '',
	import_error: false,
	import_msg: '',
	lastRowSkipQty: 0,
	downloadFile: '', //下载模板名称 必填
	configFile: '',//导入模板配置名称 必填
	uploadedShowData: false,//读取成功后是否立即显示数据 默认false
	importBtnText: '导 入',//导入按钮文字
	closeBtnText: '关 闭',//关闭按钮文字
};
const instance = new Vue({
	data() {
		return {
			disableImportBtn: true,
		};
	},
});
const genButtons = (option, options) => {
	let buttons = [];
	if (utils.isNotEmpty(options.downloadFile) || typeof options.downloadTemplate === 'function') {
		buttons.push({
			type: 'primary',
			text: '下载模板',
			click(dialog, { component }) {
                if (typeof options.downloadTemplate === 'function') {
                    options.downloadTemplate();
                    return;
                }
				typeof component.handleDownloadTemplate === 'function' && component.handleDownloadTemplate();
			},
		});
	}
	buttons.push({
		text: option.closeBtnText || '关 闭',
		click(dialog) {
			dialog.visible = false;
		},
	});
	buttons.push({
		type: 'primary',
		text: option.importBtnText || '导 入',
		disabledFn() {
			return instance.disableImportBtn;
		},
		click(dialog, { component }) {
            if (typeof component.confirm === 'function') {
                component.confirm().then(valid => {
                    if (valid) {
                        if (typeof options.importMethod === 'function') {
                            options.importMethod(component.importList, dialog, component.formData);
                            return;
                        }
                        dialog.visible = false;
                    }
                });
                return;
            }
            if (typeof options.importMethod === 'function') {
				options.importMethod(component.importList, dialog, component.formData);
				return;
			}
			dialog.visible = false;
		},
	});
	return buttons;
};
const ImportExcel = (store, router) => {
	let dialog = GlobalDialog(store, router);
	return options => {
		let option = $.extend({}, defaultProps);
		for (let field in defaultProps) {
			if (typeof options[field] !== 'undefined') {
				option[field] = options[field];
			}
		}
		let autoUpload = true;
		if (typeof options.autoUpload !== 'undefined') {
			autoUpload = options.autoUpload;
		}
		const dialogVm = dialog({
			title: options.title || '上传',
			width: options.width || '600px',
			component: {
				value: ExcelForm,
				props: {
                    rules: options.rules,
                    items: options.items,
					option,
					autoUpload,
                    labelWidth: options.labelWidth,
				},
				event(event) {
					if (event.type === 'disableImportBtn') {
						instance.disableImportBtn = event.value;
						return;
					}
					if (typeof options.event === 'function') {
						options.event(event, dialogVm);
					}
				},
			},
			buttons: genButtons(option, options),
		});
		return dialogVm;
	};
};
export default ImportExcel;
